package com.travel.server.manager.impl.command.order;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.order.QueryRightOrderListRequest;
import com.travel.server.manager.dto.response.order.QueryRightOrderListResponse;
import com.travel.server.manager.dto.response.order.QueryRightOrderListResponse.RightItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.impl.dal.OrderDal.OrderInfoParam;
import com.travel.server.manager.impl.dal.PageDto;
import com.travel.server.manager.impl.dal.RefundDal;
import com.travel.server.manager.impl.dal.RefundDal.OrderRefundParam;
import com.travel.server.manager.impl.sal.RefundSal;
import com.travel.server.manager.model.OrderRefundInfo;
import com.travel.server.manager.model.dto.OrderRefundItemDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:17
 * @Desc:
 */
@Component
@Slf4j
public class QueryRightOrderListCommand extends AbstractCommand<QueryRightOrderListRequest, QueryRightOrderListResponse> {

    @Autowired
    private RefundDal refundDal;

    @Autowired
    private OrderDal orderDal;

    @Override
    protected void checkInput(QueryRightOrderListRequest request) {
        checkPageInput(request);
    }

    @Override
    public QueryRightOrderListResponse handle(QueryRightOrderListRequest request) {
        checkPageInput(request);

        QueryRightOrderListResponse response = new QueryRightOrderListResponse();

        OrderInfoParam orderInfoParam = makeOrderParam(request);
        orderInfoParam.setRefund(true);

        if(StringUtils.isNotBlank(request.getRefundId())) {
            OrderRefundParam refundParam = new OrderRefundParam();
            refundParam.setRefundId(request.getRefundId());

            List<OrderRefundInfo> refundList = refundDal.selectByExample(refundParam);
            if(!CollectionUtil.isEmpty(refundList)){
                List<Integer> idList = new ArrayList<>();
                for(OrderRefundInfo orderRefundInfo : refundList){
                    idList.add(orderRefundInfo.getOrderId());
                }
                orderInfoParam.setIdList(idList);
            }else{
                return response;
            }
        }

        PageDto<OrderRefundItemDto> pageDto = orderDal.selectRefund(orderInfoParam);

        return makeResponse(pageDto);
    }

    private QueryRightOrderListResponse makeResponse(PageDto<OrderRefundItemDto> pageDto){
        QueryRightOrderListResponse response = new QueryRightOrderListResponse();
        List<RightItem> rightItemList = new ArrayList<>();
        response.setTotalNum(pageDto.getTotal());
        List<OrderRefundItemDto> list = pageDto.getList();
        if(!CollectionUtil.isEmpty(list)){
            for(OrderRefundItemDto refundItemDto : list){
                RightItem item = new RightItem();
                item.setId(refundItemDto.getId());
                item.setApplyTime(refundItemDto.getApplyTime());
                item.setCommodityName(refundItemDto.getCommodityName());
                item.setOrderId(refundItemDto.getOrderId());
                if(refundItemDto.getOrderAmount() != null) {
                    item.setOrderAmount(CommonUtil.fenToYuan(refundItemDto.getOrderAmount()).toString());
                }
                if(refundItemDto.getRefundAmount() != null) {
                    item.setRefundAmount(CommonUtil.fenToYuan(refundItemDto.getRefundAmount()).toString());
                }
                item.setRefundId(refundItemDto.getRefundId());
                item.setRefundState(refundItemDto.getRefundState());
                rightItemList.add(item);
            }
        }
        response.setOrderList(rightItemList);
        return response;
    }

    private OrderInfoParam makeOrderParam(QueryRightOrderListRequest request){
        OrderInfoParam orderInfoParam = new OrderInfoParam();
        if(StringUtils.isNotBlank(request.getOrderId())){
            orderInfoParam.setOrderId(request.getOrderId());
        }
        if(StringUtils.isNotBlank(request.getRefundState())){
            List<String> refundStateList = Arrays.asList(request.getRefundState());
            orderInfoParam.setRefundStateList(refundStateList);
        }
        if(StringUtils.isNotBlank(request.getStartPayTime())){
            orderInfoParam.setStartOrderTime(DateUtil.strToDate(request.getStartPayTime(), DateUtil.YYYY_MM_DD));
        }
        if(StringUtils.isNotBlank(request.getEndPayTime())){
            orderInfoParam.setEndOrderTime(DateUtil.strToDate(request.getEndPayTime(), DateUtil.YYYY_MM_DD));
        }
        if(request.getLabel() != null){
            List<String> refundStateList = RefundSal.getRefundOrderState(request.getLabel());
            if(!CollectionUtil.isEmpty(refundStateList)) {
                orderInfoParam.setRefundStateList(refundStateList);
            }
        }
        if(request.getOffset() != null && request.getLimit() != null){
            orderInfoParam.setOffset(request.getOffset());
            orderInfoParam.setLimit(request.getLimit());
        }
        return orderInfoParam;
    }
}
